package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
@Slf4j
@Controller
public class Indexcontroller {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redisson;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {

        List<CategoryEntity> entities = categoryService.getLeve1Category();
        model.addAttribute("categorys", entities);
        return "index";
    }

    @ResponseBody
    @GetMapping({"index/catalog.json"})
    public Map<String, List<Catelog2Vo>> getCatalog3() {
        Map<String, List<Catelog2Vo>> map1 = categoryService.getgetCatalog3();

        return map1;
    }

    @GetMapping({"/hello"})
    public String GetRedisson(Model model) {
        RLock lock = redisson.getLock("my-lock");

          lock.lock();//加锁 其他线程没有拿到锁就会阻塞等待
        try {
            System.out.println("加锁成功，当前线程是："+Thread.currentThread().getId());
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }


        return "hello";
    }

    @GetMapping("/write")
    @ResponseBody
    public void write(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("My_Lock");
        String s="";
        try {
            readWriteLock.writeLock().lock();
            log.info("加锁成功，当前线程为：{}",Thread.currentThread().getId());
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            stringRedisTemplate.opsForValue().set("aa",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            readWriteLock.writeLock().unlock();

        }

    }


    @GetMapping("/read")
    @ResponseBody
    public String read(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("My_Lock");


        String aa = null;
        readWriteLock.readLock().lock();

        try {
            aa = stringRedisTemplate.opsForValue().get("aa");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.readLock().unlock();

        }
        return aa;


    }


}
